
const baseUrl = 'https://jx.weidaohaosudi.com/wdh-bm/app';//喂到好精选
const httpRequest = (opts, data) => {
	let way=opts.way;
    let httpDefaultOpts = {
        url: baseUrl+'/'+opts.url,
        method: opts.method,
        header: way? {
        'X-Requested-With': 'XMLHttpRequest',
        "Accept": "application/json",
        "Content-Type": "application/json;charset=UTF-8"
    } : { 
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    },
        dataType: 'jsonp',
    }
    let promise = new Promise(function(resolve) {
        uni.request(httpDefaultOpts).then(
            (res) => {
                var data=JSON.parse(res[1].data);
                if(data.code=="1000"||data.code=="1001"){
					resolve(data)
					
                	// uni.reLaunch({
                	// 	url: '/pages/login/login'
                	// })
                }else{
                	resolve(data)
                }
            }
        ).catch(
            (response) => {
                console.log(response);
            }
        )
    })
    return promise
};
//带Token请求
const httpTokenRequest = (opts, data) => {
	let token = "";

	if(uni.getStorageSync('token')){
		token=uni.getStorageSync('token');
	}
	
    //此token是登录成功后后台返回保存在storage中的
	let way=opts.way;
    let httpDefaultOpts = {
        url: baseUrl+'/'+opts.url,
        data: data,
        method: opts.method,
        header: way ? {
		'token': token,
        'X-Requested-With': 'XMLHttpRequest',
        "Accept": "application/json",
        "Content-Type": "application/json;charset=UTF-8"
    } : {
		'token': token,
        'X-Requested-With': 'XMLHttpRequest',
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
    },
        dataType: 'jsonp',
    }
    let promise = new Promise(function(resolve) {
        uni.request(httpDefaultOpts).then(
            (res) => {
				var data=JSON.parse(res[1].data);
				if(data.code=="1000"||data.code=="1001"){
					resolve(data)
					// uni.reLaunch({
					// 	url: '/pages/login/login'
					// })
				}else{
					resolve(data)
				}
                
            }
        ).catch(
            (response) => {
				// resolve(response)
                console.log(response);
            }
        )
    })
    return promise
};
 
export default {
	    baseUrl,
		httpRequest,
		httpTokenRequest
}
